package com.anyisn.anyflickr.shell.view
{
	import com.anyisn.anyflickr.modules.PhotoInfo.PhotoInfoModuleFacade;
	import com.anyisn.anyflickr.modules.PhotosSlide.PhotosSlideModuleFacade;
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.puremvc.as3.multicore.utilities.pipes.component.PipeAwareModule;
	import com.anyisn.puremvc.as3.multicore.utilities.pipes.event.ModuleEvents;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeAware;
	import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
	import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.JunctionMediator;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;
	import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeSplit;
	
	public class ApplicationJunctionMediator extends JunctionMediator
	{
		public static const NAME:String = 'AnyFlickrJunctionMediator';
		
		public function ApplicationJunctionMediator()
		{
			super(NAME, new Junction());
		}
		
		override public function onRegister() : void {
			junction.registerPipe(PipeAwareModule.APP_TO_MODULE_PIPE, Junction.OUTPUT, new TeeSplit());
			junction.registerPipe(PipeAwareModule.MODULE_TO_APP_PIPE, Junction.INPUT, new TeeMerge());
			junction.addPipeListener(PipeAwareModule.MODULE_TO_APP_PIPE, this, handlePipeMessage);
		}
		
		override public function handlePipeMessage(message:IPipeMessage) : void {
			// Handle our Module -> Application integration
			if(message.getBody() is INotification) {
				var note:INotification = message.getBody() as INotification;
				switch(note.getName()) {
					case PhotoInfoModuleFacade.EXPORT_PHOTO_INFO_WINDOW:
						sendNotification(ShellFacade.SHOW_PHOTO_INFO_WINDOW, note.getBody(), note.getType());
						break;
					case PhotosSlideModuleFacade.EXPORT_PHOTOS_SLIDE_WINDOW:
						sendNotification(ShellFacade.SHOW_PHOTOS_SLIDE_WINDOW, note.getBody(), note.getType());
						break;
					default:
						sendNotification(note.getName(), note.getBody(), note.getType());
						break;
				}
			}
		}
		
		override public function listNotificationInterests() : Array {
			var interests:Array = super.listNotificationInterests();
			interests.push(ModuleEvents.CONNECT_MODULE_JUNCTION);
			
			/*interests.push(ApplicationFacade.INTERESTINGNESS_GET_LIST_SUCCESS);
			interests.push(ApplicationFacade.FAVORITES_GET_LIST_SUCCESS);
			interests.push(ApplicationFacade.PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS);
			interests.push(ApplicationFacade.PHOTOS_SEARCH_SUCCESS);*/
			
			interests.push(ModuleEvents.MODULE_JUNCTION_CONNCTED);
			/*interests.push(ApplicationFacade.SETTINGS_SAVE);
			interests.push(ApplicationFacade.LOGIN_SUCCESS);
			interests.push(ApplicationFacade.LOGOUT_SUCCESS);
			interests.push(ShellFacade.TO_CLOSE_PHOTO_INFO_WINDOW);*/
			interests.push(ShellFacade.CHANGE_PHOTO_IN_PHOTO_INFO_WINDOW);
			return interests;
		}
		
		override public function handleNotification(note:INotification) : void {
			switch(note.getName()) {
				/*case ApplicationFacade.INTERESTINGNESS_GET_LIST_SUCCESS:
				case ApplicationFacade.FAVORITES_GET_LIST_SUCCESS:
				case ApplicationFacade.PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS:
				case ApplicationFacade.PHOTOS_SEARCH_SUCCESS:
				
				case ApplicationFacade.LOGIN_SUCCESS:
					var msg:Message = new Message(note.getName(), null, note);
					junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE, msg);
					break;*/
				case ModuleEvents.CONNECT_MODULE_JUNCTION:
					var module:IPipeAware = note.getBody() as IPipeAware;
					
//					create the pipe
					var moduleToApp:Pipe = new Pipe();
//					connect the pipe to our module
					module.acceptOutputPipe(PipeAwareModule.MODULE_TO_APP_PIPE, moduleToApp);
//					connect the pipe to our app
					var appIn:TeeMerge = junction.retrievePipe(PipeAwareModule.MODULE_TO_APP_PIPE) as TeeMerge;
					appIn.connectInput(moduleToApp);
					
//					create the pipe
					var appToModule:Pipe = new Pipe();
//					connect the pipe to our module
					module.acceptInputPipe(PipeAwareModule.APP_TO_MODULE_PIPE, appToModule);
//					connect the pipe to our app
					var appOut:TeeSplit = junction.retrievePipe(PipeAwareModule.APP_TO_MODULE_PIPE) as TeeSplit;
					appOut.connect(appToModule);
					
					sendNotification(ModuleEvents.MODULE_JUNCTION_CONNCTED, note.getBody());
					break;
				case ModuleEvents.MODULE_JUNCTION_CONNCTED:
					/*var widget:Module = note.getBody() as Module;
					switch(widget.className) {
						case PhotosSlideWidget.NAME:
							var proxy:SettingsProxy = facade.retrieveProxy(SettingsProxy.NAME) as SettingsProxy;
							var noti:Notification = new Notification(PhotosSlideFacade.DELAY_SET, proxy.slideDelay);
							var msg:Message = new Message(PhotosSlideFacade.DELAY_SET, null, noti);
							junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE, msg);
							break;
					}*/
					break;
//				case ShellFacade.TO_CLOSE_PHOTO_INFO_WINDOW:
				case ShellFacade.CHANGE_PHOTO_IN_PHOTO_INFO_WINDOW:
					var msg:Message = new Message(note.getName(), null, note);
					junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE, msg);
					break;
				default:
					super.handleNotification(note);
			}
		}
	}
}